package com.frontend.screens;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;

import com.frontend.ConnectionException;
import com.frontend.DoctorsOfficeClient;
import com.users.*;

import java.util.*;

/**
 * 
 * @author Elizabeth
 * @author Taylor
 * 
 */
public class SearchScreen extends Screen {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1868205419321633836L;
	private JLabel title, message, output, nameLabel, classLabel, unameLabel;
	private JPanel topPanel, b1, b2, b3, b4, p, psub, extraButtonPanel, buttonsPanel, labelPanel;
	private JTextField inputField;
	private JList list;
	private JScrollPane jsp;
	private JButton search, delUsr, chpass, unlock, suspend,
			createDoc, back, overview;
	private User[] uList;
	private MyCellRenderer renderer;

	public SearchScreen(DoctorsOfficeClient window) {
		super(window);

		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		SearchListener sl = new SearchListener(this);

		b1 = new JPanel();
		b1.setOpaque(false);

		title = new JLabel("User Directory Search");
		title.setAlignmentX(CENTER_ALIGNMENT);

		b2 = new JPanel();
		b2.setOpaque(false);

		topPanel = new JPanel();
		topPanel.setOpaque(false);
		topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.X_AXIS));
		message = new JLabel("Name:");
		inputField = new JTextField();
		inputField.addActionListener(sl);
		search = new JButton("Search");
		search.addActionListener(sl);
		search.setOpaque(false);
		topPanel.add(message);
		topPanel.add(inputField);
		topPanel.add(search);

		b3 = new JPanel();
		b3.setOpaque(false);

		JPanel lp = new JPanel();
		lp.setOpaque(false);
		labelPanel = new JPanel();
		labelPanel.setOpaque(false);
		lp.add(labelPanel);
		labelPanel.setLayout(new GridLayout(1, 3));
		nameLabel = new JLabel("Name:");
		classLabel = new JLabel("User Type:");
		unameLabel = new JLabel("Username:");

		labelPanel.add(nameLabel);
		labelPanel.add(classLabel);
		labelPanel.add(unameLabel);

		output = new JLabel("Results:");
		output.setAlignmentX(CENTER_ALIGNMENT);

		list = new JList();
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		list.setLayoutOrientation(JList.VERTICAL);
		list.setVisibleRowCount(-1);
		list.setAlignmentX(Component.CENTER_ALIGNMENT);
		renderer = new MyCellRenderer(new Font("Arial", Font.PLAIN,
				getHeight() / 20),getWidth(), getHeight());
		list.setCellRenderer(renderer);
		list.addListSelectionListener(new ListListener());
		list.setMaximumSize(new Dimension(getWidth()*4/5, getHeight()/2));

		jsp = new JScrollPane(list);
		jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		jsp.setMaximumSize(new Dimension(getWidth()*4/5, getHeight()/2));

		p = new JPanel();
		p.setAlignmentX(Component.CENTER_ALIGNMENT);
		p.setOpaque(false);

		psub = new JPanel();
		psub.setLayout(new BorderLayout());
		psub.setBackground(Color.red);
		psub.setPreferredSize(new Dimension(200, 200));
		psub.setOpaque(true);

		p.add(psub);
		psub.add(jsp, BorderLayout.CENTER);

		b4 = new JPanel();
		b4.setOpaque(false);

		delUsr = new JButton("Delete User");
		delUsr.setOpaque(false);
		delUsr.addActionListener(sl);
		delUsr.setMnemonic(KeyEvent.VK_D);

		chpass = new JButton("Change Password");
		chpass.setOpaque(false);
		chpass.addActionListener(sl);
		chpass.setMnemonic(KeyEvent.VK_P);

		unlock = new JButton("Unlock");
		unlock.setOpaque(false);
		unlock.addActionListener(sl);
		unlock.setMnemonic(KeyEvent.VK_U);

		suspend = new JButton("Suspend");
		suspend.setOpaque(false);
		suspend.addActionListener(sl);
		suspend.setMnemonic(KeyEvent.VK_S);

		createDoc = new JButton("Create Document");
		createDoc.setOpaque(false);
		createDoc.addActionListener(sl);
		createDoc.setMnemonic(KeyEvent.VK_C);

		back = new JButton("Back");
		back.setOpaque(false);
		back.addActionListener(sl);
		back.setMnemonic(KeyEvent.VK_B);
		
		overview = new JButton("View Patient Information and Documents");
		overview.setOpaque(false);
		overview.addActionListener(sl);
		overview.setMnemonic(KeyEvent.VK_O);

		extraButtonPanel = new JPanel();
		extraButtonPanel.setOpaque(false);
		
		extraButtonPanel.add(overview);
		
		buttonsPanel = new JPanel();
		buttonsPanel.setOpaque(false);

		if (window.getCurrentUser().getUser_type() == User.ADMIN) {
			buttonsPanel.add(delUsr);
			buttonsPanel.add(chpass);
			buttonsPanel.add(unlock);
			buttonsPanel.add(suspend);
			buttonsPanel.add(back);
		} else {
			buttonsPanel.add(delUsr);
			buttonsPanel.add(createDoc);
			buttonsPanel.add(back);
		}

		add(b1);
		add(title);
		add(b2);
		add(topPanel);
		add(b3);
		add(output);
		add(lp);
		add(p);
		add(b4);
		add(extraButtonPanel);
		add(buttonsPanel);

		search();
		inputField.requestFocus();
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		if (getHeight() != tempheight || getWidth() != tempwidth) {
//			System.out.println(getWidth() + ", " + getHeight());
			title.setFont(new Font("Arial", Font.BOLD, getHeight() / 15));
			message.setFont(new Font("Arial", Font.PLAIN, getHeight() / 25));
			inputField.setMaximumSize(new Dimension(getWidth() / 2,
					getHeight() / 20));
			inputField.setFont(new Font("Arial", Font.PLAIN, getHeight() / 25));
			search.setMaximumSize(new Dimension(getWidth() / 5,
					getHeight() / 20));
			search.setFont(new Font("Arial", Font.PLAIN, getHeight() / 25));

			nameLabel.setFont(new Font("Arial", Font.PLAIN, getHeight() / 25));
			classLabel.setFont(new Font("Arial", Font.PLAIN, getHeight() / 25));
			unameLabel.setFont(new Font("Arial", Font.PLAIN, getHeight() / 25));
			labelPanel.setPreferredSize(new Dimension(getWidth() * 4 / 5,
					getHeight() / 20));

			output.setFont(new Font("Arial", Font.BOLD, getHeight() / 20));
			psub.setPreferredSize(new Dimension(getWidth() * 4 / 5,
					getHeight() * 1 / 2));
			list.setCellRenderer(new MyCellRenderer(new Font("Arial",
					Font.PLAIN, getHeight() / 30),getWidth(), getHeight()));
			list.setMaximumSize(new Dimension(getWidth()*4/5, getHeight()/2));
			jsp.setMaximumSize(new Dimension(getWidth()*4/5, getHeight()/2));


			overview.setMaximumSize(new Dimension(getWidth() / 5,
					getHeight() / 25));
			overview.setFont(new Font("Arial", Font.PLAIN, getHeight() / 30));
			delUsr.setMaximumSize(new Dimension(getWidth() / 5,
					getHeight() / 25));
			delUsr.setFont(new Font("Arial", Font.PLAIN, getHeight() / 30));
			chpass.setMaximumSize(new Dimension(getWidth() / 5,
					getHeight() / 25));
			chpass.setFont(new Font("Arial", Font.PLAIN, getHeight() / 30));
			unlock.setMaximumSize(new Dimension(getWidth() / 5,
					getHeight() / 25));
			unlock.setFont(new Font("Arial", Font.PLAIN, getHeight() / 30));
			suspend.setMaximumSize(new Dimension(getWidth() / 5,
					getHeight() / 25));
			suspend.setFont(new Font("Arial", Font.PLAIN, getHeight() / 30));
			createDoc.setMaximumSize(new Dimension(getWidth() / 5,
					getHeight() / 25));
			createDoc.setFont(new Font("Arial", Font.PLAIN, getHeight() / 30));
			back.setMaximumSize(new Dimension(getWidth() / 5, getHeight() / 25));
			back.setFont(new Font("Arial", Font.PLAIN, getHeight() / 30));

			tempheight = getHeight();
			tempwidth = getWidth();
		}
	}

	private class ListListener implements
			javax.swing.event.ListSelectionListener {

		@Override
		public void valueChanged(ListSelectionEvent e) {
			if (list.getSelectedIndex() != -1) {
				User selected = uList[list.getSelectedIndex()];
				delUsr.setEnabled(true);
				chpass.setEnabled(true);
				createDoc.setEnabled(true);
				unlock.setEnabled(true);
				suspend.setEnabled(true);
				overview.setEnabled(true);
				if (window.getCurrentUser().getUser_type() != User.ADMIN) {
					if (selected.getUser_type() != User.PATIENT) {
						createDoc.setEnabled(false);
						delUsr.setEnabled(false);
					}
				} else {
					if (selected.getUser_type() == User.ADMIN
							&& !selected.equals(window.getCurrentUser())) {
						chpass.setEnabled(false);
					} else if (selected.getUser_type() == User.ADMIN
							&& selected.equals(window.getCurrentUser())) {
						suspend.setEnabled(false);
						unlock.setEnabled(false);
						delUsr.setEnabled(false);
					}
				}
				if(selected.getUser_type() != User.PATIENT){
					overview.setEnabled(false);
				}
			}
		}

	}

	private void search() {

		User[] resultsList = null;
		try {
			resultsList = window.getNet().searchNames(inputField.getText());
		} catch (ConnectionException e) {
			System.out.println(e.getMessage());
		}
		if (resultsList != null) {
			Arrays.sort(resultsList);
			uList = resultsList;
			String[][] data = new String[resultsList.length][3];
			for (int i = 0; i < data.length; i++)
				data[i] = resultsList[i].getValsAsArr();
			list.setListData(data);
		}
	}

	private class SearchListener implements ActionListener {
		private SearchScreen superClass;
		
		public SearchListener(SearchScreen superClass){
			super();
			this.superClass = superClass;
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource().equals(back)) {
				window.changeScreen(new NavigationScreen(window));
			} else if(e.getSource().equals(chpass) && list.getSelectedIndex() != -1){
				String username = ((String[]) (list.getSelectedValue()))[2];
				window.changeScreen(new ChangePasswordScreen(window, username, superClass));
			}else if (e.getSource().equals(unlock)
					&& list.getSelectedIndex() != -1) {
				String username = ((String[]) (list.getSelectedValue()))[2];
				try {
					window.getNet().unlock(username);
				} catch (ConnectionException e1) {
					System.out.println(e1.getMessage());
				}
				uList[list.getSelectedIndex()].setSuspended(false);
				list.requestFocus();
				repaint();
			} else if (e.getSource().equals(suspend)
					&& list.getSelectedIndex() != -1) {
				String username = ((String[]) (list.getSelectedValue()))[2];
				try {
					window.getNet().suspend(username);
				} catch (ConnectionException e1) {
					System.out.println(e1.getMessage());
				}
				uList[list.getSelectedIndex()].setSuspended(true);
				list.requestFocus();
				repaint();
			} else if (e.getSource().equals(delUsr)
					&& list.getSelectedIndex() != -1) {
				String username = ((String[]) (list.getSelectedValue()))[2];

				if (JOptionPane.showOptionDialog(window,
						"Are you sure you want to delete " + username
								+ "?\nThis operation cannot be undone!",
						"Confirm Delete", JOptionPane.OK_CANCEL_OPTION,
						JOptionPane.WARNING_MESSAGE, null, null, null) == JOptionPane.OK_OPTION) {

					try {
						window.getNet().deleteUser(username);
					} catch (ConnectionException e1) {
						System.out.println(e1.getMessage());
					}
				}
			} else if (e.getSource().equals(overview)
					&& list.getSelectedIndex() != -1){
				window.changeScreen(new PatientOverviewScreen(window, uList[list.getSelectedIndex()], superClass));
			}

			if (e.getSource().equals(search)
					|| e.getSource().equals(inputField)
					|| e.getSource().equals(delUsr)) {
				search();
			}
		}
	}

	private class MyCellRenderer extends JPanel implements ListCellRenderer {
		/**
		 * 
		 */
		private static final long serialVersionUID = 2985185169127969497L;
		/**
		 * 
		 */
		JLabel left, middle, right;
		private Font renderFont;
		private int width, height;

		MyCellRenderer(Font renderFont, int width, int height) {
			this.width = width;
			this.height = height;
			setLayout(new GridLayout(1, 3));
			setMaximumSize(new Dimension(width*4/5, height));
			left = new JLabel();
			middle = new JLabel();
			right = new JLabel();
			left.setOpaque(true);
			middle.setOpaque(true);
			right.setOpaque(true);
			add(left);
			add(middle);
			add(right);
			this.renderFont = renderFont;
		}

		public Component getListCellRendererComponent(JList list, Object value,
				int index, boolean isSelected, boolean cellHasFocus) {
			String leftData = ((String[]) value)[0];
			String middleData = ((String[]) value)[1];
			String rightData = ((String[]) value)[2];
			left.setText(leftData);
			middle.setText(middleData);
			right.setText(rightData);
			
			left.setMaximumSize(new Dimension(width*4/15, height));
			middle.setMaximumSize(new Dimension(width*4/15, height));
			right.setMaximumSize(new Dimension(width*4/15, height));
			
			if (isSelected) {
				left.setBackground(list.getSelectionBackground());
				left.setForeground(list.getSelectionForeground());
				middle.setBackground(list.getSelectionBackground());
				middle.setForeground(list.getSelectionForeground());
				right.setBackground(list.getSelectionBackground());
				right.setForeground(list.getSelectionForeground());
			} else {
				left.setBackground(list.getBackground());
				left.setForeground(list.getForeground());
				middle.setBackground(list.getBackground());
				middle.setForeground(list.getForeground());
				right.setBackground(list.getBackground());
				right.setForeground(list.getForeground());
			}
			setEnabled(list.isEnabled());
			left.setFont(renderFont);
			middle.setFont(renderFont);
			right.setFont(renderFont);
			if (uList[index].isSuspended() && !isSelected) {
				left.setBackground(Color.pink);
				middle.setBackground(Color.pink);
				right.setBackground(Color.pink);
			}
			if (uList[index].isSuspended() && isSelected) {
				Color mag = new Color(255, 51, 51);
				left.setBackground(mag);
				middle.setBackground(mag);
				right.setBackground(mag);
			}
			return this;
		}
	}

}
